﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using DeJC.DAL.Model;
using DeJC.BLL;

namespace WEB.Infrastructure
{
    public class EmailManager
    {
        private const string EmailFrom = "jose.taveras@gmail.com";
        public static void SendConfirmationEmail(string userName)
        {
            var user = Membership.GetUser(userName.ToString());

            if (user != null)
            {
                Usuario loggedUserInfo;
                using (var unitOfWork = new UnitOfWork())
                {
                    loggedUserInfo = unitOfWork.UsuarioRepository.GetUsuario(userName);
                }

                var verifyUrl = HttpContext.Current.Request.Url.GetLeftPart
                                    (UriPartial.Authority) + "/Account/Verify?ActivationCode=" + loggedUserInfo.CodigoActivacion;

                using (var client = new SmtpClient())
                {
                    using (var message = new MailMessage(EmailFrom, user.Email))
                    {
                        message.Subject = "Please Verify your Account";
                        message.Body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + user.UserName +
                                       ", </p><p>To verify your account, please click the following link:</p>"
                                       + "<p><a href=\"" + verifyUrl + "\" target=\"_blank\">" + verifyUrl
                                       + "</a></p><div>Best regards,</div><div>Someone</div><p>Do not forward "
                                       + "this email. The verify link is private.</p></body></html>";

                        message.IsBodyHtml = true;

                        client.EnableSsl = true;
                        
                        //Comentado hasta que sepa la configuracion para enviar el mail
                        //client.Send(message);
                    }
                }
            }
        }
    }
}